OPEN COM Statement ---------------------------------------------------------------------------- Action Opens and initializes a communications channel for I-O. Syntax OPEN " COM n. optlist1 optlist2" FOR mode AS # filenum% LEN= reclen% Remarks The OPEN COM statement must be executed before a device can be used for communication using an RS232 interface. COM n is the name of the device to be opened. If there are any syntax errors in the OPEN COM statement, BASIC generates the error message Bad file name. The OPEN COM statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- n The communications port to open, such as 1 for COM1 and 2 for COM2. optlist1 The most-often-used communications parameters. The defaults are 300 baud, even parity, 7 data bits, and 1 stop bit. The syntax of optlist1 and options are described later in this entry. optlist2 Up to 10 other optional, Argument Description ---------------------------------------------------------------------------- optlist2 Up to 10 other optional, less-often-used data communications parameters. The parameters for optlist2 are described later in this entry. mode One of the keywords OUTPUT, INPUT, or RANDOM (the default). filenum% Any unused file number between 1 and 255, inclusive. reclen% The size of a random-access-mode buffer (128 bytes is the default). The argument optlist1 specifies the communication-line parameters and has this syntax. speed , parity , data , stop The following table lists the valid values for optlist1 options. ----------------------------------------------------------------------------- Option Description Range Default ---------------------------------------------------------------------------- speed Baud rate (bits per 75, 110, 150, 300, 300 second) of the 600, 1200, 1800, device to be opened) 2400, 9600 parity Method of parity N, E, O, S, M E checking (none, even, odd, space, mark) data Number of data bits 5, 6, 7, or 8 7 per byte Option Description Range Default ---------------------------------------------------------------------------- stop Number of stop bits 1, 1.5, or 2 1 * * The default value is 1 for baud rates greater than 110. For baud rates less than or equal to 110, the default value is 1.5 when data is 5; otherwise, the value is 2. Options in this list must be entered in the order shown. Use comma placeholders for defaults. For example. OPEN "COM1. ,N,8," FOR INPUT AS #1 Only the baud rates shown are supported. Any other value for speed is invalid. If any options from optlist2 are chosen, comma placeholders still must be used even if all of the optlist1 options are defaults. For example. OPEN "COM1. ,,,,CD1500" FOR INPUT AS #1 If you set data to 8 bits per byte, you must specify no parity ( N). Note Because BASIC uses complete bytes (8 bits) for numbers, you must specify 8 data bits when transmitting or receiving numeric data. The optlist2 options can be specified in any order, and must be separated by commas. There are three types of options. data mode, buffer size, and handshaking. The data-mode options ( ASC, BIN, and LF) are described in the following table. ----------------------------------------------------------------------------- Option Description ---------------------------------------------------------------------------- ASC Opens the device in ASCII mode. In ASCII mode, tabs are expanded to blanks, carriage returns are forced at end-of-line, and Ctrl+Z means end-of-file. When the Option Description ---------------------------------------------------------------------------- means end-of-file. When the channel is closed, Ctrl+Z is sent over the RS-232 line. BIN Opens the device in binary mode. This option supersedes the LF option. BIN is elected by default unless ASC is specified. In binary mode, tabs are not expanded to spaces, a carriage return is not forced at the end of a line, and Ctrl+Z is not treated as end-of-file. When the channel is closed, Ctrl+Z will not be sent over the RS232 line. LF Allows communication files to be printed on a serial line printer. Option Description ---------------------------------------------------------------------------- printed on a serial line printer. Effective only with the ASC option. When LF is specified, a line-feed character (0AH) is automatically sent after each carriage-return character (0DH). This includes the carriage return sent as a result of the width setting. Note that INPUT and LINE INPUT, when used to read from a communications file that was opened with the LF option, stop when they see a carriage return, ignoring the line feed. The buffer-size options for sequential modes (RB and TB) are described in the following table. ----------------------------------------------------------------------------- Option Description ---------------------------------------------------------------------------- RB n Sets the size of the receive buffer to n bytes. The initial buffer size, if n or the RB option is omitted, is 512 bytes, unless overridden by the -C option on the QBX or BC command line. The default value, if n or the RB option is omitted, is the current receive-buffer size. The maximum size is 32,767 bytes. TB n Sets the size of the receive buffer to n bytes. The initial buffer size, if n or the TB option is omitted, is 512 bytes. The default value, if n or the TB option is omitted, is the current receive-buffer size. Option Description ---------------------------------------------------------------------------- receive-buffer size. Handshake and timing options (RS, CD, CS, DS, and OP) are described in the table below. ----------------------------------------------------------------------------- Option Description ---------------------------------------------------------------------------- RS Suppresses detection of Request To Send (RTS). CD m Specifies the timeout period on the Data Carrier Detect line (DCD). If no signal appears on the DCD line (the DCD line remains low) Option Description ---------------------------------------------------------------------------- line (the DCD line remains low) for more than m milliseconds, a device timeout occurs. If a CD timeout occurs, ERDEV contains 130 (82H). The range for m, if specified, is 0 - 65,535 milliseconds, inclusive. The default value is 0, if the CD option is not used, or if m is omitted. When m is 0, either by default or assignment, it means that the state of the DCD line is to be ignored. CS m Specifies the timeout period on the Clear To Send line (CTS). If no signal appears on the CTS line (the CTS line remains low) for Option Description ---------------------------------------------------------------------------- (the CTS line remains low) for more than m milliseconds, a device timeout occurs. If a CS timeout occurs, ERDEV contains 128 (80H). The range for m, if specified, is 0 - 65,535 milliseconds, inclusive. The default value is 1,000 milliseconds, if the CS option is not used, or if m is omitted. When m is 0, either by default or assignment, it means the state of the CTS line is to be ignored. DS m Specifies the timeout period on the Data Set Ready line (DSR). If no signal appears on the DSR line (the DSR line remains low) for Option Description ---------------------------------------------------------------------------- (the DSR line remains low) for more than m milliseconds, a device timeout occurs. If a DS timeout occurs, ERDEV contains 129 (81H). The range for m, if specified, is 0 - 65,535 milliseconds, inclusive. (See Note below.) The default value is 1,000 milliseconds, if the DS option is not used, or if m is omitted. When m is 0, either by default or assignment, it means that the state of the DSR line is to be ignored. OP m Specifies how long the OPEN statement waits for all communications lines to become Option Description ---------------------------------------------------------------------------- communications lines to become active. The range for m is 0 - 65,535 milliseconds, inclusive. The default value for m, if the OP option is not used, is 10 times the CD or DS timeout value, whichever is greater. If OP is specified, but with m omitted, OPEN COM waits for 10 seconds. Use a relatively large value for the OP option compared to the CS, DS, or CD options. Note Under OS-2, specifying DS0 to ignore the state of the Data Set Ready (DSR) line does not work properly. In this case, you will have to either not ignore the DSR line or you will have to jumper the DSR line to an active high signal line. Refer to serial port information that specifically pertains to your hardware, and perform any modifications at your own risk. The argument filenum% is the number used to open the file. The argument reclen% is effective only in random-access mode and specifies the length of the random-access buffer (default is 128 bytes). You can use any of the random-access I-O statements, such as GET and PUT, to treat the device as if it were a random-access file. The argument mode is one of the following string expressions. ----------------------------------------------------------------------------- Mode Description ---------------------------------------------------------------------------- OUTPUT Specifies sequential output mode. Mode Description ---------------------------------------------------------------------------- OUTPUT Specifies sequential output mode. INPUT Specifies sequential input mode. RANDOM Specifies random-access mode. If mode is omitted, it is assumed to be random-access input-output. The OPEN COM statement performs the following steps in opening a communications device. - The communications buffers are allocated and interrupts are enabled. - The Data Terminal Ready line (DTR) is set high. - If either of the OP or DS options is nonzero, the statement waits for the timeout period for the Data Set Ready line (DSR) to go high. If a timeout occurs, the process goes to step 6. - If the RS option is not specified, the Request To Send line (RTS) is set high. - If either of the OP or CD options is nonzero, the statement waits for the timeout period for the Data Carrier Detect line (DCD) to go high. If a timeout occurs, the process goes to step 6. Otherwise, the RS232 device has been successfully opened. - If there is a timeout, the open fails. The process deallocates the buffers, disables interrupts, clears all of the control lines, and generates the message Device timeout. In addition, for DOS, the process sets the value of ERDEV$ to COM and sets ERDEV to a value that indicates the signal line that timed out, according to the following table. ----------------------------------------------------------------------------- ERDEV value Signal line ---------------------------------------------------------------------------- 128 (80H) Clear to Send (CTS) timeout ERDEV value Signal line ---------------------------------------------------------------------------- 128 (80H) Clear to Send (CTS) timeout 129 (81H) Data Set Ready (DSR) timeout 130 (82H) Data Carrier Detect (DCD) timeout Note If there is not an OPEN COM statement in your program, you can reduce the size of the .EXE file by linking your program with the stub file NOCOM.OBJ. Example See the COM statements programming example, which uses the OPEN COM statement.